package ${basePackageName}.interfaces.assembler;

<#if common>
import com.zy.explore.common.infrastructure.util.DataBeanUtil;
import com.zy.explore.common.interfaces.frm.CommonIdFrm;
import com.zy.explore.common.interfaces.frm.CommonIdsFrm;
import com.zy.explore.common.interfaces.vo.PageVO;
<#else>
import ${basePackageName}.infrastructure.util.DataBeanUtil;
import ${basePackageName}.interfaces.frm.CommonIdFrm;
import ${basePackageName}.interfaces.frm.CommonIdsFrm;
import ${basePackageName}.interfaces.vo.PageVO;
</#if>
import ${basePackageName}.interfaces.validator.<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}Validator;
import ${basePackageName}.domain.mybatis.entity.<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix};
import ${basePackageName}.interfaces.frm.${tableClassNamePrefix}SaveFrm;
import ${basePackageName}.interfaces.frm.${tableClassNamePrefix}QueryFrm;
import ${basePackageName}.interfaces.vo.${tableClassNamePrefix}VO;

import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;
<#if pkType != 'String'>
import java.util.stream.Collectors;
import java.util.Objects;
</#if>

/**
 * description 表(${tableName})对应tk.mybatis的Assembler类
 *
 * @author ${authorName}
 * @date ${dateTime}
 */
@Component
public class <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}Assembler {

    /**
     * 新增数据
     *
     * @param frm 参数对象
     * @return 视图对象
     */
    public ${tableClassNamePrefix}VO insert(${tableClassNamePrefix}SaveFrm frm) {
        <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} entity = convertFromFrm(frm);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return convertToVO(entity.save());
    }

    /**
     * 删除对象
     *
     * @param frm 参数对象
     * @return true:删除成功 false:删除失败
     */
    public Integer delete(CommonIdFrm frm) {
    <#if pkType == 'Integer'>
        return <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Integer.parseInt(frm.get${pkPropNameFirstUpper}())).build().remove();
    <#elseif pkType == 'Long'>
        return <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Long.parseLong(frm.get${pkPropNameFirstUpper}())).build().remove();
    <#elseif pkType == 'String'>
        return <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(frm.get${pkPropNameFirstUpper}()).build().remove();
    </#if>
    }

    /**
     * 更新对象
     *
     * @param frm 待更新的Frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public ${tableClassNamePrefix}VO update(${tableClassNamePrefix}SaveFrm frm) {
        <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} entity = convertFromFrm(frm);
        entity.setUpdateTime(new Date());
        return convertToVO(entity.updateSelective());
    }

    /**
     * 根据ID查询数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public ${tableClassNamePrefix}VO findById(CommonIdFrm frm) {
    <#if pkType == 'Integer'>
        return convertToVO(<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Integer.parseInt(frm.get${pkPropNameFirstUpper}())).build().findById());
    <#elseif pkType == 'Long'>
        return convertToVO(<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Long.parseLong(frm.get${pkPropNameFirstUpper}())).build().findById());
    <#elseif pkType == 'String'>
        return convertToVO(<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().${pkPropName}(frm.get${pkPropNameFirstUpper}()).build().findById());
    </#if>
    }

    /**
     * 通过主键批量查询数据集合
     *
     * @param frm 主键集合Frm对象
     * @return 成功返回对应ID集合的VO对象集合，失败返回空集合
     */
    public List<${tableClassNamePrefix}VO> findByIds(CommonIdsFrm frm) {
        List<${tableClassNamePrefix}VO> retList = new ArrayList<>();
        if (CollectionUtils.isEmpty(frm.getIds())) {
            return retList;
        }
    <#if pkType == 'Integer'>
        List<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}> list = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds().stream().map(Integer::parseInt).collect(Collectors.toList()));
    <#elseif pkType == 'Long'>
        List<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}> list = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds().stream().map(Long::parseLong).collect(Collectors.toList()));
    <#elseif pkType == 'String'>
        List<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}> list = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds());
    </#if>
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
        }
        return retList;
    }

    /**
     * 根据条件查询数据集合
     *
     * @param frm 查询条件对象
     * @return 满足条件的数据集合
     */
    public List<${tableClassNamePrefix}VO> findList(${tableClassNamePrefix}QueryFrm frm) {
        List<${tableClassNamePrefix}VO> retList = new ArrayList<>();
        Example example = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.getExample(frm);
        List<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}> list = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().build().findList(example);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
        }
        return retList;
    }

    /**
     * 根据条件查询分页
     *
     * @param frm 查询条件
     * @return 满足条件的分页
     */
    public PageVO<${tableClassNamePrefix}VO> findPage(${tableClassNamePrefix}QueryFrm frm) {
        // 组装条件、排序、分页
        Example example = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.getExample(frm);
        // 查询分页
        PageInfo<<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}> pageInfo = <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.builder().build().findPageInfo(example, frm.getPageNum(), frm.getPageSize());
        // 转成PageVO
        PageRequest pageRequest = PageRequest.of(frm.getPageNum() - 1, frm.getPageSize());
        List<${tableClassNamePrefix}VO> retList = new ArrayList<>();
        if (pageInfo == null) {
            return new PageVO<>(new PageImpl<>(retList, pageRequest, 0L));
        }
        if (!CollectionUtils.isEmpty(pageInfo.getList())) {
            pageInfo.getList().forEach(x -> retList.add(convertToVO(x)));
        }
        return new PageVO<>(new PageImpl<>(retList, pageRequest, pageInfo.getTotal()));
    }

    /**
     * Frm 转 实体类
     *
     * @param frm 参数对象
     * @return 返回转换后的实体对象
     */
    public <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}convertFromFrm(${tableClassNamePrefix}SaveFrm frm) {
        // 新增或修改时根据实际业务做参数校验
        <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}Validator.validateFrm(this, frm);
        <#if pkType == 'String'>
        return DataBeanUtil.copyOf(frm, <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.class, true);
        <#elseif pkType == 'Integer'>
        <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} entity = DataBeanUtil.copyOf(frm, <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.class, true);
        if (!Objects.equals(frm.get${pkPropNameFirstUpper}(), null)) {
            entity.set${pkPropNameFirstUpper}(Integer.parseInt(frm.get${pkPropNameFirstUpper}()));
        }
        return entity;
        <#elseif pkType == 'Long'>
        <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} entity = DataBeanUtil.copyOf(frm, <#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix}.class, true);
        if (!Objects.equals(frm.get${pkPropNameFirstUpper}(), null)) {
            entity.set${pkPropNameFirstUpper}(Long.parseLong(frm.get${pkPropNameFirstUpper}()));
        }
        return entity;
        </#if>
    }

    /**
     * 实体类 转 VO
     *
     * @param entity 实体类对象
     * @return 返回转化后的VO对象
     */
    public ${tableClassNamePrefix}VO convertToVO(<#if prefixTk??>${prefixTk}</#if>${tableClassNamePrefix} entity) {
    <#if pkType == 'String'>
        return entity == null ? null : DataBeanUtil.copyOf(entity, ${tableClassNamePrefix}VO.class, true);
    <#elseif pkType == 'Integer' || pkType == 'Long'>
        if (entity == null) {
            return null;
        }
        ${tableClassNamePrefix}VO vo = DataBeanUtil.copyOf(entity, ${tableClassNamePrefix}VO.class, true);
        if (!Objects.equals(entity.get${pkPropNameFirstUpper}(), null)) {
            vo.set${pkPropNameFirstUpper}(entity.get${pkPropNameFirstUpper}().toString());
        }
        return vo;
    </#if>
    }
}
